﻿using dbapi_lib.API.Persistence.DAO;
using dbapi_lib.API.Persistence.Model;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Reflection;

namespace Handball_plugin_lib
{
    class TeamDAO : AbstractDAO
    {
        /// <summary>
        ///  Private static fields declaration. Field names must be lower case and the id must be type of INTEGER PRIMARY KEY ASC.
        ///  Possible types:
        ///  <ul>
        ///     <li>INTEGER</li> 
        ///     <li>DOUBLE</li>
        ///     <li>VARCHAR()</li>
        ///     <li>DATETIME</li>
        ///     <li>BOOLEAN</li>
        /// </ul>
        /// </summary>
        //TODO set up the field description
        private static String[] fieldNames = new[] { "id",                      "name",        "founded",  "isbanned" };
        private static String[] fieldTypes = new[] { "INTEGER PRIMARY KEY ASC", "VARCHAR(20)", "DATETIME", "BOOLEAN" };

        /// <summary>
        /// The table name for this DAO.
        /// </summary>
        //TODO set up table name
        private static string tableName = "teams";

        protected override string getDatabaseTableName()
        {
            return tableName;
        }

        protected override List<string> getDatabaseTableFieldNames()
        {
            return fieldNames.OfType<String>().ToList();
        }

        protected override List<string> getDatabaseTableFieldTypes()
        {
            return fieldTypes.OfType<String>().ToList();
        }

        //TODO implement this method as needed
        protected override BaseMO getRowAsMO(SQLiteDataReader recset)
        {
            //TODO set MO type
            TeamMO teammo = new TeamMO();
            Object value = new Object();

            for (int i = 0; i < fieldNames.Length; i++)
            {
                String name = fieldNames[i];
                String type = fieldTypes[i];

                Object o = recset[name];

                if (name.Equals("id"))
                {
                    value = Convert.ToInt32(o);
                }
                else
                {
                    switch (type.Substring(0, 5))
                    {
                        case "INTEG": value = Convert.ToInt32(o);
                            break;
                        case "VARCH": value = Convert.ToString(o);
                            break;
                        case "DATET": value = Convert.ToDateTime(o);
                            break;
                        case "DOUBL": value = Convert.ToDouble(o);
                            break;
                        case "BOOLE": value = Convert.ToBoolean(o);
                            break;
                    }
                }

                PropertyInfo[] properties = teammo.GetType().GetProperties();

                foreach (PropertyInfo iter in properties)
                {
                    if (iter.Name == name)
                    {
                        iter.SetValue(teammo, value, null);
                    }
                }
            }

            recset.Close();
            return teammo;
        }

        //Other MO specific public methods for use in other parts of the software.

        public int save(TeamMO mo)
        {
            int id = insertReturnPK(mo);
            return id;
        }

        public void save(List<TeamMO> q)
        {
            foreach (TeamMO mo in q)
            {
                save(mo);
            }
        }

        public List<TeamMO> getAllMO()
        {
            SQLiteDataReader reader = getAll();

            List<TeamMO> list = new List<TeamMO>();

            while (reader.Read())
            {
                list.Add((TeamMO)getRowAsMO(reader));
            }

            return list;
        }

        public TeamMO findById(int id)
        {
            return (TeamMO)getRowAsMO(find(id));
        }
    }
}
